﻿using NLog;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;

namespace XmlServer.Hosting.Service
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {
        private System.ServiceProcess.ServiceProcessInstaller swInstaller;
        private System.ServiceProcess.ServiceInstaller serviceInstaller;

        private static Logger logger = LogManager.GetCurrentClassLogger();

        public ProjectInstaller()
        {
            InitializeComponent();
            InstallService();
        }


        public void InstallService()
        {
            try
            {
                logger.Info(String.Format("{0} is installing", Helpers.Common.ServiceName));

                swInstaller = new System.ServiceProcess.ServiceProcessInstaller();
                serviceInstaller = new System.ServiceProcess.ServiceInstaller();


                swInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                swInstaller.Password = null;
                swInstaller.Username = null;

                serviceInstaller.ServiceName = Helpers.Common.ServiceName;
                serviceInstaller.Description = Helpers.Common.Description;
                serviceInstaller.AfterInstall += new InstallEventHandler(serviceInstaller_AfterInstall);
                serviceInstaller.AfterUninstall += new InstallEventHandler(serviceInstaller_AfterUninstall);

                this.Installers.AddRange(new Installer[] { swInstaller, serviceInstaller });
            }
            catch (Exception err)
            {
                logger.Error(err, String.Format("Failed to install service '{0}'", Helpers.Common.ServiceName));
            }
        }

        void serviceInstaller_AfterUninstall(object sender, InstallEventArgs e)
        {

            // write the uninstall message to the event viewer
            try
            {
                Helpers.Common.LogEvent(Helpers.Common.ServiceName, "Application",
                    String.Format("Successfully uninstalled service '{0}'", Helpers.Common.ServiceName), System.Diagnostics.EventLogEntryType.SuccessAudit);
            }
            catch { }

            // write the message to a log file
            try
            {
                logger.Info(String.Format("{0} was Uninstalled", Helpers.Common.ServiceName));
            }
            catch (Exception err)
            {                
                logger.Error(err, "Failed in AfterUninstall service");               
            }
        }

        void serviceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            // write the install message to the event viewer
            try
            {
                Helpers.Common.LogEvent(Helpers.Common.ServiceName, "Application",
                    String.Format("Successfully installed service '{0}'", Helpers.Common.ServiceName), System.Diagnostics.EventLogEntryType.SuccessAudit);
            }
            catch { }


            // write the message to a log file
            try
            {
                logger.Info(String.Format("{0} was Installed", Helpers.Common.ServiceName));
            }
            catch (Exception err)
            {
                logger.Error(err, "Failed in AfterInstall service");
            }
        }

    }
}
